package cn.demo.client.api;


import cn.demo.act.domain.ActActivityInfo;
import cn.demo.act.domain.ActRegister;
import cn.demo.act.service.IActActivityInfoService;
import cn.demo.act.service.IActRegisterService;
import cn.demo.common.core.controller.BaseController;
import cn.demo.common.core.domain.R;
import cn.demo.common.core.page.PageQuery;
import cn.demo.common.core.page.PageR;
import cn.demo.common.core.redis.RedisCache;
import cn.demo.common.utils.LockUtils;
import cn.demo.common.utils.SecurityUtils;
import cn.demo.common.utils.ServletUtils;
import cn.demo.common.utils.StringUtils;
import cn.demo.common.utils.uuid.IdUtils;
import cn.demo.common.utils.uuid.UUID;
import cn.demo.weixin.ma.constant.WxMaConstants;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xtjun
 * @description 内容api
 * @date 2024/06/04
 */
@Tag(name = "内容api")
@RestController
@RequestMapping("/api/act")
public class ActApi extends BaseController {
    @Resource
    IActActivityInfoService actActivityInfoService;
    @Resource
    IActRegisterService actRegisterService;
    @Autowired
    private RedisCache redisCache;

    @Operation(summary = "查询活动列表")
    @GetMapping("/list")
    public PageR<ActActivityInfo> list(ActActivityInfo actActivityInfo, PageQuery pageQuery) {
        String deptId = ServletUtils.getHeader(WxMaConstants.HEADER_DEPT_ID);
        if (StringUtils.isNotEmpty(deptId)){
            actActivityInfo.setDeptId(deptId);
        }
        Page<ActActivityInfo> page = actActivityInfoService.selectActActivityInfoPage(actActivityInfo, pageQuery);
        return PageR.ok(page);
    }

    @Operation(summary = "查询活动详情")
    @GetMapping("/detail")
    public R<ActActivityInfo> detail(String id) {
        ActActivityInfo actActivityInfo = actActivityInfoService.selectActActivityInfoById(id);
        actActivityInfo.setIsApply(actRegisterService.count(QueryWrapper.create().eq(ActRegister::getActivityId, id).eq(ActRegister::getUserId, SecurityUtils.getUserId())) > 0);
        return R.ok(actActivityInfo);
    }

    @Operation(summary = "活动报名")
    @PostMapping("/apply")
    public R apply(@RequestBody @Validated ActRegister actRegister) {
        actRegister.setUserId(SecurityUtils.getUserId());
        return LockUtils.lockFun("act:apply:" + actRegister.getActivityId() + ":" + actRegister.getUserId(), IdUtils.fastSimpleUUID(), () -> {
            long count = actRegisterService.count(QueryWrapper.create().eq(ActRegister::getActivityId, actRegister.getActivityId()).eq(ActRegister::getUserId, actRegister.getUserId()));
            if (count > 0) {
                return R.fail("您已报名此活动，请勿重复报名！");
            }
            ActActivityInfo actInfo = actActivityInfoService.getById(actRegister.getActivityId());
            if (actInfo == null) {
                return R.fail("活动不存在！");
            }
            if (count >= actInfo.getParticipantsNumber()) {
                return R.fail("报名人数已满！");
            }
            return toAjax(actRegisterService.insertActRegister(actRegister));
        });
    }

    @Operation(summary = "活动取消报名")
    @DeleteMapping("/applyCancel")
    public R applyCancel(@RequestBody ActRegister actRegister) {
        return toAjax(actRegisterService.remove(QueryWrapper.create().eq(ActRegister::getActivityId, actRegister.getActivityId()).eq(ActRegister::getUserId, SecurityUtils.getUserId())));
    }
}
